
// TOKEN操作
const key = 'WEB-SURVEY';

const schoolCodeKey = 'SCHOOL-CODE';

const schoolNameKey = 'SCHOOL-NAME';

export const API_SUCCESS_CODE = '0';

export const BASE_API_VERSION = 'v2';

export const API_VERSION = 'v1';

export const moduleName = 'survey';

export const getToken = () => localStorage.getItem(`refresh-${key}`) || localStorage.getItem(key) || getUrlParam('token');

export const setToken = token => localStorage.setItem(key, token);

export const setRefreshToken = token => localStorage.setItem(`refresh-${key}`, token);

export const removeToken = () => localStorage.removeItem(key);

export const removeRefreshToken = () => localStorage.removeItem(`refresh-${key}`);

export const getSchoolCode = () => getUrlParam('schoolCode') || localStorage.getItem(schoolCodeKey);

export const setSchoolCode = schoolCode => localStorage.setItem(schoolCodeKey, schoolCode);

export const getSchoolName = () => getUrlParam('schoolName') || localStorage.getItem(schoolNameKey);

export const setSchoolName = schoolName => localStorage.setItem(schoolNameKey, schoolName);

// 下载文件
export const downFile = (blob, fileName) => {
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(blob, decodeURI(fileName))
  } else {
    var link = document.createElement('a')
    link.href = URL.createObjectURL(blob)
    link.download = decodeURI(fileName)
    document.body.appendChild(link)
    link.click()
  }
}

export const previewFile = (blob, fileName) => {
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(blob, fileName);
  } else {
    let link = document.createElement('a');
    link.href = URL.createObjectURL(blob);
    link.target = '_blank';
    document.body.appendChild(link);
    link.click();
  }
}
export const iosPreviewFile = (blob) => {
  window.location.replace(URL.createObjectURL(blob))
}
export const filterObj = (obj) => {
  let newObj = {};
  for (const key in obj) {
    if (obj[key] || obj[key] === 0 || obj[key] === false) {
      newObj[key] = obj[key];
    }
    if (Object.prototype.toString.call(obj[key]) === '[object Object]' && JSON.stringify(obj[key]) !== '{}') {
      newObj[key] = filterObj(obj[key]);
    }
  }
  return newObj;
}
export const getUrlParam = (name) => {
  let url = window.location.href;
  let p = url.split('?')[1];
  let params = new URLSearchParams(p)
  return params.get(name)
}